package com.ruibo.modules.system.controller;

import com.ruibo.aop.log.Log;
import com.ruibo.constants.UserConstants;
import com.ruibo.exception.BadRequestException;
import com.ruibo.modules.system.criteria.RoleQueryCriteria;
import com.ruibo.modules.system.service.dto.SysRoleDto;
import com.ruibo.modules.system.service.SysRoleService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * (SysRole)表控制层
 *
 * @author sunfeng
 * @since 2020-04-17 13:34:58
 */
@RestController
@RequestMapping("/system/sysRole")
@AllArgsConstructor
public class SysRoleController {
    /**
     * 服务对象
     */

    private final SysRoleService sysRoleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SysRoleDto selectOne(Long id) {
        return this.sysRoleService.queryById(id);
    }

    /**
     * 获取角色列表
     *
     * @param criteria
     * @param pageable
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@el.check('system:role:list')")
    @Log("查询角色列表")
    public ResponseEntity<Object> getRoles(RoleQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(sysRoleService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@el.check('system:role:query')")
    @GetMapping(value = "/{roleId}")
    public ResponseEntity<SysRoleDto> getInfo(@PathVariable Long roleId) {
        return new ResponseEntity<>(sysRoleService.queryById(roleId), HttpStatus.OK);
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@el.check('system:role:edit')")
    @PutMapping("/changeStatus")
    public ResponseEntity changeStatus(@RequestBody SysRoleDto role) {

        return new ResponseEntity<>(sysRoleService.updateRoleStatus(role), HttpStatus.OK);
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@el.check('system:role:add')")
    @PostMapping
    public ResponseEntity add(@Validated @RequestBody SysRoleDto role) {

        if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(role))) {
            return new ResponseEntity<Object>("新增角色'" + role.getRoleName() + "'失败，角色名称已存在", HttpStatus.INTERNAL_SERVER_ERROR);

        } else if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(role))) {
            return new ResponseEntity<Object>("新增角色'" + role.getRoleName() + "'失败，角色权限已存在", HttpStatus.INTERNAL_SERVER_ERROR);

        }
        return new ResponseEntity<>(sysRoleService.insert(role), HttpStatus.OK);
    }

    /**
     * 修改保存角色
     */
    @PreAuthorize("@el.check('system:role:edit')")
    @PutMapping
    public ResponseEntity edit(@Validated @RequestBody SysRoleDto role) {
        sysRoleService.checkRoleAllowed(role);
        if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(role))) {
            throw new BadRequestException("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(role))) {
            throw new BadRequestException("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        return new ResponseEntity<>(sysRoleService.update(role), HttpStatus.OK);
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@el.check('system:role:remove')")
    @DeleteMapping("/{roleIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] roleIds) {
        sysRoleService.deleteById(roleIds);

        return new ResponseEntity<>(HttpStatus.OK);
    }

}
